package com.flavors.wan.main.ui.Fragment;

import android.util.Log;
import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.flavors.wan.main.bean.BannerBean;
import com.flavors.wan.main.ui.BaseFragment;
import com.flavors.wan.main.ui.LoadingFragment;
import com.flavors.wan.main.view.GlideImageLoader;
import com.flavors.wan.main.vm.WanVM;
import com.plat.sdk.R;
import com.plat.sdk.okgo.callbck.XmResource;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link OneFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class OneFragment extends BaseFragment {
    @BindView(R.id.fragment_content)
    FrameLayout fragment_content;

    private final String LOADING_FRAGMENT_TAG = LoadingFragment.class.getName();
    private FragmentManager mFragmentManager;
    private Banner banner;
    private WanVM wanVM;


    public OneFragment() {
    }

    public static OneFragment newInstance() {
        OneFragment fragment = new OneFragment();
        return fragment;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_one;
    }

    @Override
    protected void initView() {
        mFragmentManager = getFragmentManager();
        banner = findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
    }

    @Override
    public void onVisible(boolean isFirstVisible) {
        super.onVisible(isFirstVisible);
        if (isFirstVisible) {
            Log.d("ydong","isFirstVisible loadData()");
            loadData();
        }
    }

    /**
     * 第一次由不可见到可见加载数据
     */
    protected void loadData() {
        wanVM = ViewModelProviders.of(this).get(WanVM.class);
        wanVM.fetchBanner();
        wanVM.getBanners().observe(this, new Observer<XmResource<List<BannerBean>>>() {
            @Override
            public void onChanged(XmResource<List<BannerBean>> listXmResource) {
                listXmResource.handle(new SimpleOnHandleCallBack<List<BannerBean>>() {
                    @Override
                    public void onLoading() {
                        super.onLoading();
                        showLoading();
                    }

                    @Override
                    public void onSuccess(List<BannerBean> data) {
                        dismissLoading();
                        List<String> images = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            String imagePath = data.get(i).getImagePath();
                            images.add(imagePath);
                        }
                        //设置图片集合
                        banner.setImages(images);
                        //banner设置方法全部调用完毕时最后调用
                        banner.start();
                    }

                    @Override
                    public void onError(int code, String message) {
                        super.onError(code, message);
                        dismissLoading();
                    }

                    @Override
                    public void onFailure(String message) {
                        super.onFailure(message);
                        dismissLoading();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        dismissLoading();
                    }
                });

            }
        });

    }

    private void showLoading() {
        Fragment fmt = mFragmentManager.findFragmentByTag(LOADING_FRAGMENT_TAG);
        if (fmt != null)
            return;
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content, new LoadingFragment(), LOADING_FRAGMENT_TAG)
                .commit();
    }

    private void dismissLoading() {
        Fragment fmt = mFragmentManager.findFragmentByTag(LOADING_FRAGMENT_TAG);
        if (fmt != null) {
            mFragmentManager.beginTransaction()
                    .remove(fmt)
                    .commit();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
